package com.czmh.etc.onlinepubsys.controller.cmg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czmh.etc.onlinepubsys.constants.cmg.BusinessType;
import com.czmh.etc.onlinepubsys.enums.cmg.ApiCodeEnum;
import com.czmh.etc.onlinepubsys.json.cmg.JSONResult;
import com.czmh.etc.onlinepubsys.request.cmg.*;
import com.czmh.etc.onlinepubsys.response.cmg.ApiResponseBase;
import com.czmh.etc.onlinepubsys.utils.cmg.CmgHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/{version}/api/cmg/apply")
@Api(tags = "申办卡签接口相关")
public class CmgApplyController {
    @Value("${cmg.accessno}")
    private String accessno;

    @PostMapping("/addCustomerInfo")
    @ApiOperation("录入ETC客户信息:个人用户返回用户编号，单位用户返回为空字符串")
    public JSONResult addCustomerInfo(@RequestBody AddCustomerInfoRequest req) {
        JSONResult result = new JSONResult();
        log.info("addCustomerInfo:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.ADD_CUSTOMER_INFO, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/addOrUpdateBankSign")
    @ApiOperation("录入线下银行签约信息:申请成功返回签约交易流水号")
    public JSONResult addOrUpdateBankSign(@RequestBody AddOrUpdateBankSignRequest req) {
        JSONResult result = new JSONResult();
        log.info("addOrUpdateBankSign:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        req.setSignChannelId(accessno + time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.ADD_OR_UPDATE_BANK_SIGN, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/addVehicle")
    @ApiOperation("车辆信息录入:申请成功返回签约交易流水号")
    public JSONResult addVehicle(@RequestBody AddVehicleRequest req) {
        JSONResult result = new JSONResult();
        log.info("addVehicle:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.ADD_VEHICLE, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/addAgent")
    @ApiOperation("录入经办人信息:单位用户返回经办人编码")
    public JSONResult addAgent(@RequestBody AddAgentRequest req) {
        JSONResult result = new JSONResult();
        log.info("addAgent:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.ADD_AGENT, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/cancelBusiness")
    @ApiOperation("业务单取消接口")
    public JSONResult cancelBusiness(@RequestBody CancelBusinessRequest req) {
        JSONResult result = new JSONResult();
        log.info("cancelBusiness:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.CANCEL_BUSINESS, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/checkVehilce")
    @ApiOperation("车辆卡签校验查询接口(本省)：响应码1007:该车在外省（本省XX机构）发行过卡签\n" +
            "1008:该车在本机构发行过卡\n" +
            "1009:该车在本机构发行过签\n" +
            "1010:该车在本机构发行不存在\n" +
            "1030:该车在本机构发行过卡与签")
    public JSONResult checkVehilce(@RequestBody CheckVehilceRequest req) {
        JSONResult result = new JSONResult();
        log.info("checkVehilce:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.CHECK_VEHILCE, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/checkAndQueryVehilce")
    @ApiOperation("车辆卡签校验查询接口(全国和本省)：" +
            "用户需要先开户才能校验，如果返回车辆信息为空，则只支持车辆信息修改，如果车辆信息不为空，支持新增\n" +
            "1010:该车在本机构发行不存在")
    public JSONResult checkAndQueryVehilce(@RequestBody CheckAndQueryVehilceRequest req) {
        JSONResult result = new JSONResult();
        log.info("checkAndQueryVehilce:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.CHECK_AND_QUERY_VEHILCE, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/getBusinessDetail")
    @ApiOperation("申办订单详情")
    public JSONResult getBusinessDetail(@RequestBody GetBusinessDetailRequest req) {
        JSONResult result = new JSONResult();
        log.info("getBusinessDetail:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.GET_BUSINESS_DETAIL, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/queryCustomerInfo")
    @ApiOperation("查询ETC客户信息:如果返回客户信息，只支持用户修改信息，不支持新增,如果用户是单位用户(证件类型5~10)且\n" +
            "接口未返回用户经办人信息，则需录入单位经办人信息")
    public JSONResult queryCustomerInfo(@RequestBody QueryCustomerInfoRequest req) {
        JSONResult result = new JSONResult();
        log.info("queryCustomerInfo:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.QUERY_CUSTOMER_INFO, req);
        JSONObject jsonObject = new JSONObject();
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
            }
        } else if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.USER_EXIST.getCode())) {
            jsonObject.put("state", 0);
            result.setCode(r.getCode());
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
            result.setRspData(JSON.toJSONString(jsonObject));
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/queryVehilce")
    @ApiOperation("车辆信息查询:如果返回车辆信息为空，则只支持车辆信息修改，如果车辆信息不为空，支持新增")
    public JSONResult queryVehilce(@RequestBody QueryVehilceRequest req) {
        JSONResult result = new JSONResult();
        log.info("queryVehilce:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.QUERY_VEHILCE, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/submitBusiness")
    @ApiOperation("申办订单提交")
    public JSONResult submitBusiness(@RequestBody SubmitBusinessRequest req) {
        JSONResult result = new JSONResult();
        log.info("submitBusiness:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.SUBMIT_BUSINESS, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/updateCustomerInfo")
    @ApiOperation("更新ETC客户信息:个人用户返回用户编号,经办人返回为空字符串")
    public JSONResult updateCustomerInfo(@RequestBody UpdateCustomerInfoRequest req) {
        JSONResult result = new JSONResult();
        log.info("updateCustomerInfo:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.UPDATE_CUSTOMER_INFO, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

    @PostMapping("/updateVehicle")
    @ApiOperation("更新车辆信息")
    public JSONResult updateVehicle(@RequestBody UpdateVehicleRequest req) {
        JSONResult result = new JSONResult();
        log.info("updateVehicle:{}", JSON.toJSONString(req));
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        ApiResponseBase r = CmgHttpUtil.post(BusinessType.UPDATE_VEHICLE, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

}
